package itbour.onetouchshow.utils.gsonanalysis;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;
import java.util.List;

import itbour.onetouchshow.bean.inching.noppt.InchingCoreRootNoPPTV10Bean;
import itbour.onetouchshow.bean.inching.noppt.InchingRelationsV10Bean;
import itbour.onetouchshow.bean.inching.noppt.NoPPTRootImageValueV10Bean;
import itbour.onetouchshow.bean.inching.noppt.NoPPTRootShapeValueV10Bean;
import itbour.onetouchshow.bean.inching.noppt.NoPPTRootTableValueV10Bean;
import itbour.onetouchshow.bean.inching.noppt.NoPPTRootTextValueV10Bean;
import itbour.onetouchshow.bean.inching.noppt.NoPPTRootVideoValueV10Bean;

/**
 * Created by zjl_d on 2017/11/22.
 */

public class CoreObjToJsonNoPPTRules implements JsonSerializer<InchingCoreRootNoPPTV10Bean> {
    @Override
    public JsonElement serialize(InchingCoreRootNoPPTV10Bean obj, Type typeJson, JsonSerializationContext context) {
        //在这里判断是否有relations 如果有就加没有就不加
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("ct", obj.getCt());
        jsonObject.addProperty("id", obj.getId());
        jsonObject.addProperty("topic", obj.getTopic());


        jsonObject.addProperty("child_max", obj.getChild_max());
        jsonObject.addProperty("h", obj.getH());
        jsonObject.addProperty("o", obj.getO());
        jsonObject.addProperty("p", obj.getP());

        //添加relations
        JsonArray jsonArrayRelations = new JsonArray();

        List<InchingRelationsV10Bean> relations = obj.getRelations();
        if (relations != null) {
            for (int i = 0; i < relations.size(); i++) {
                JsonElement coreStructureJson = context.serialize(relations.get(i), InchingRelationsV10Bean.class);
                jsonArrayRelations.add(coreStructureJson);
            }
            jsonObject.add("relations", jsonArrayRelations);

        }

        if (obj.getPage_index() == null) {

        } else {
            jsonObject.addProperty("page_index", obj.getPage_index());
        }

        if (obj.getObj_id() == null) {
        } else {
            jsonObject.addProperty("obj_id", obj.getObj_id());
        }


        //用于遍历添加子child
        JsonArray jsonArray = new JsonArray();
        List<InchingCoreRootNoPPTV10Bean> children = obj.getChildren();
        if (children != null) {
            for (int i = 0; i < children.size(); i++) {
                JsonElement coreStructureJson = context.serialize(children.get(i), InchingCoreRootNoPPTV10Bean.class);
                jsonArray.add(coreStructureJson);
            }
        }
        jsonObject.add("children", jsonArray);

        jsonObject.addProperty("kind", obj.getKind());
        Integer type = obj.getType();
        jsonObject.addProperty("type", type);
        //用于添加value
        Object value = obj.getValue();

        //1代表文字类型
        if (type == InchingCoreRootNoPPTV10Bean.SJGNodeTypeText) {
            JsonElement coreStructureValueJson = context.serialize(value, NoPPTRootTextValueV10Bean.class);
            jsonObject.add("value", coreStructureValueJson);
        } else if (type == InchingCoreRootNoPPTV10Bean.SJGNodeTypeImage) {//2代表图片
            JsonElement coreStructureValueJson = context.serialize(value, NoPPTRootImageValueV10Bean.class);
            jsonObject.add("value", coreStructureValueJson);
        } else if (type == InchingCoreRootNoPPTV10Bean.SJGNodeTypeImage) {//4代表表格
            JsonElement coreStructureValueJson = context.serialize(value, NoPPTRootTableValueV10Bean.class);
            jsonObject.add("value", coreStructureValueJson);
        } else if (type == InchingCoreRootNoPPTV10Bean.SJGTGNodeModeTypeShape) {//5代表图形
            JsonElement coreStructureValueJson = context.serialize(value, NoPPTRootShapeValueV10Bean.class);
            jsonObject.add("value", coreStructureValueJson);
        } else if (type == InchingCoreRootNoPPTV10Bean.SJGNodeTypeVideo) {
            JsonElement coreStructureValueJson = context.serialize(value, NoPPTRootVideoValueV10Bean.class);
            jsonObject.add("value", coreStructureValueJson);
        }


        InchingCoreRootNoPPTV10Bean child_template = obj.getChild_template();
        if (child_template != null) {
            JsonElement coreStructureRootValueJson = context.serialize(child_template, InchingCoreRootNoPPTV10Bean.class);
            jsonObject.add("child_template", coreStructureRootValueJson);
        }


        return jsonObject;
    }
}
